package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLabResultEntity;
import com.dhcc.cdc.busi.service.ICdcLabResultService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 检验结果
 * 
 * @ClassName: LabResultController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:21:40
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/labresult")
public class CdcLabResultController extends BaseController {

	private String PREFIX = "/backend/cdc/labresult/";

	@Autowired
	private ICdcLabResultService labResultService;
	

    /**
     * 跳转到检验结果列表首页
     */
    @RequestMapping("")
    @Permission("labresult:list")
    public String index() {
        return PREFIX + "labresult.html";
    }
	
    /**
     * 跳转到新增检验结果
     */
    @RequestMapping("/to_add")
    @Permission("labresult:add")
    public String toAdd() {
        return PREFIX + "labresult_add.html";
    }
	
	/**
     * 跳转到修改检验结果
     */
    @RequestMapping("/to_update/{labResultId}")
    @Permission("labresult:update")
    public String toUpdate(@PathVariable Long labResultId, Model model) {
        CdcLabResultEntity labResult = this.labResultService.selectById(labResultId);
        model.addAttribute("labResult",labResult);
        return PREFIX + "labresult_edit.html";
    }

    /**
     * 获取检验结果列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("labresult:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcLabResultEntity> result = null;
        Page<CdcLabResultEntity> page = new PageFactory<CdcLabResultEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<CdcLabResultEntity>();
        }else{
            result = this.labResultService.page(page,params);
        }
        page.setRecords((List<CdcLabResultEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增检验结果
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("labresult:add")
    @BussinessLog(value = "新增检验结果")
    public Object add(CdcLabResultEntity labResult) {
        if (CommonUtil.isOneEmpty(labResult)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        labResult.insert();
        return R.ok();
    }

    /**
     * 删除检验结果
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("labresult:delete")
    @BussinessLog(value = "删除检验结果")
    public Object delete(@RequestParam Long labResultId) {
	
        this.labResultService.deleteById(labResultId);

        return R.ok();
    }

    /**
     * 修改检验结果
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("labresult:update")
    @BussinessLog(value = "修改检验结果")
    public Object update(CdcLabResultEntity labResult) {
        if (CommonUtil.isOneEmpty(labResult)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcLabResultEntity old = this.labResultService.selectById(labResult.getId());

        old.setLabReportId(labResult.getLabReportId());
        old.setLabItemId(labResult.getLabItemId());
        old.setResult(labResult.getResult());
        old.setResultType(labResult.getResultType());
        old.setResultNote(labResult.getResultNote());
        old.setAbnormalDesc(labResult.getAbnormalDesc());
        old.setResultUnit(labResult.getResultUnit());
        old.setResultRange(labResult.getResultRange());
        old.setResultMdr(labResult.getResultMdr());
        old.setDataId(labResult.getDataId());
        old.setIsActive(labResult.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
